package com.lastproject.ejb.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.TopicDTO;
import com.lastproject.dto.TopicWithNamesDTO;
import com.lastproject.ejb.entities.Category;
import com.lastproject.ejb.entities.Subcategory;
import com.lastproject.ejb.entities.Topic;
import com.lastproject.ejb.util.CategoryConversion;
import com.lastproject.ejb.util.SubcategoryConversion;
import com.lastproject.ejb.util.TopicConversion;

/**
 * Session Bean implementation class TopicDAO author : Zier Erik
 */
@Stateless
public class TopicDAO implements TopicDAORemote {

	@PersistenceContext(unitName = "LastProjectEJB")
	private EntityManager em;
	@EJB
	TopicConversion conv;
	
	@EJB
	CategoryConversion catConv;
	
	@EJB
	SubcategoryConversion subConv;

	public TopicDAO() {
	}

	@Override
	public void create(TopicDTO entity) {
		Topic topic = new Topic();
		topic = conv.fromDTO(entity);
		em.persist(topic);
	}

	@Override
	public void edit(TopicDTO entity) {
		em.merge(conv.fromDTO(entity));
	}

	@Override
	public void remove(TopicDTO entity) {
		Topic topic = em.find(Topic.class, conv.fromDTO(entity).getId());
		em.remove(topic);
	}

	@Override
	public TopicDTO findById(int id) {
		Topic topic = em.find(Topic.class, id);
		return conv.fromEntity(topic);
	}

	@Override
	public List<TopicDTO> findAll() {

		List<Topic> topics = em.createNamedQuery("Topic.findAll", Topic.class)
				.getResultList();

		List<TopicDTO> topicdtolist = new ArrayList<TopicDTO>();

		for (Topic topic : topics) {

			TopicDTO tDto = new TopicDTO();
			tDto = conv.fromEntity(topic);
			topicdtolist.add(tDto);
		}

		return topicdtolist;
	}

	@Override
	public TopicDTO findByCatAndSubcat(int id, int id2) {
		// TODO Auto-generated method stub
		Category cat = new Category();
		cat = em.find(Category.class, id);

		Subcategory subcat = new Subcategory();
		subcat = em.find(Subcategory.class, id2);

		if (em.createNamedQuery("Topic.findByCatAndSubcat", Topic.class)
				.setParameter("cat", cat).setParameter("subcat", subcat)
				.getResultList().size() == 0)
			return null;

		return conv.fromEntity(em
				.createNamedQuery("Topic.findByCatAndSubcat", Topic.class)
				.setParameter("cat", cat).setParameter("subcat", subcat)
				.getResultList().get(0));
	}

	@Override
	public List<TopicWithNamesDTO> findAllWithNames() {
		List<Topic> tList = em.createNamedQuery("Topic.findAll", Topic.class).getResultList();
		List<TopicWithNamesDTO> tDTOList = new ArrayList<TopicWithNamesDTO>();
		for (Topic t: tList){
			TopicWithNamesDTO tDTO = new TopicWithNamesDTO();
			tDTO.setId(t.getId());
			tDTO.setCategoryDTO(catConv.fromEntity(t.getCategory()));
			tDTO.setSubcategoryDTO(subConv.fromEntity(t.getSubcategory()));
			
			tDTOList.add(tDTO);
		}
		return tDTOList;
	}
}
